home *** CD-ROM | disk | FTP | other *** search
-
- /*******************************************************************************
- ********************************************************************************
- ********************************************************************************
-
- PERMISSION TO COPY THIS SOFTWARE IS HEREBY GIVEN BY THE AUTHOR PROVIDED THAT
- THIS LEADING MESSAGE IS INCLUDED IN ALL OF THE RELEVANT SOURCE FILES.
-
- P. SCHMITZ, UNIVERSITY OF KEELE, MAY 1988.
-
-
- ********************************************************************************
- ********************************************************************************
- *******************************************************************************/
-
- #include "files.h"
- #include "ships.h"
- #include <stdio.h>
- #include <signal.h>
- #include <sgtty.h>
-
- #define ON 1
- #define OFF 0
-
- FILE *f, *fopen();
- /*variables for extracting termcap info*/
- char cm[20];
- char cl[20];
- char bon[20];
- char boff[20];
- int shipnumber,x,y,tty_type;
- int nointro, termcap;
- struct sgttyb params;
- char masterpidname[15];
- char *tnam;
- int statflg;
- int fd,pid,masterpid,status;
- char terminalfile[14];
- extern char *ttyname();
- extern void slaveoutc();
- char *p;
- struct scores {
- int reason;
- int sno;
- char name[10];
- int score;
- } sb[50];
-
- readinscores()
- {
- int reason,score,i;
- char string[50];
- char *getpwuid(), *getlogin();
- char c;
-
- if ((f=fopen(PLAYERSCOREFILE,"r"))!=NULL)
- {
- fgets(string,50,f);
- sscanf(string,"%d %d\n",&reason,&score);
- fclose(f);
- sleep(3);
- clrscreenslave(cl,tty_type);
- } else
- {
- sleep(3);
- clrscreenslave(cl,tty_type);
- fprintf(stdout,"Two games were created at once. Please try again. That should work.\n");
- return(0);
- }
-
- for (i=0;i<50;++i)
- {
- sb[i].reason=0;
- sb[i].sno=0;
- for (y=0;y<10;y++)
- sb[i].name[y]=' ';
- sb[i].score=0;
- }
-
- if ((f=fopen(SCOREFILE,"r"))!=NULL)
- {
- for (i=0;i<50;++i)
- {
- fgets(string,50,f);
- sscanf(string,"%d %d %s %d\n",&sb[i].sno,&sb[i].reason,sb[i].name,&sb[i].score);
- }
- fclose(f);
- }
- i=0;
- while ((i<50)&&(score<=sb[i].score)) ++i;
-
- if (i>=50)
- {
- fprintf(stdout,"\nYou did so badly, that you didn't even make the scoreboard!\n");
- unlink(PLAYERSCOREFILE);
- return(0);
- }
-
- x=48;
- while(x>(i-1))
- {
- sb[x+1].score=sb[x].score;
- sb[x+1].sno=sb[x].sno;
- sb[x+1].reason=sb[x].reason;
- for (y=0;y<10;y++)
- sb[x+1].name[y]=sb[x].name[y];
- x--;
- }
-
- sb[i].reason=reason;
- sb[i].score=score;
- for (y=0;y<10;y++)
- sb[i].name[y]=' ';
-
- if ((p=getlogin())==NULL)/*can we get it from /etc/utmp?*/
- {
- if ((p=getpwuid(getuid()))==NULL)/*can we get it from /etc/utmp?*/
- {
- write(2, "Can't find your logname. Who Are You?\n",39);
- unlink(PLAYERSCOREFILE);
- return(0);
- }
- }
-
- strcpy(sb[i].name,p);
- sb[i].sno=shipnumber;
- /*write out scoreboard*/
- unlink(SCOREFILE);
- x=creat(SCOREFILE,0666);
- if ((f=fopen(SCOREFILE,"w"))!=NULL)
- {
- for (x=0;x<50;++x)
- {
- sprintf(string,"%d %d %s %d\n",sb[x].sno,sb[x].reason,sb[x].name,sb[x].score);
- fputs(string,f);
- }
- fclose(f);
- }
- unlink(PLAYERSCOREFILE);
-
- /*display scores*/
- fprintf(stdout,"\n");
- tputs(bon,1,slaveoutc);
- fprintf(stdout,"*****************************************************************************\n");
- fprintf(stdout,"* MULTI-PLAYER-STAR-TREK's ALL TIME HIGHS *\n");
- fprintf(stdout,"*****************************************************************************\n");
- tputs(boff,1,slaveoutc);
- fprintf(stdout,"*Position Name Ships name Cause of Death Score *\n");
- fprintf(stdout,"*****************************************************************************\n");
- y=i+6;
- for (x=(i-6); x<y; ++x)
- {
- if (x>49) break;
- if (x<0) {y++;
- continue;
- }
- if (x==i)
- {
- tputs(bon,1,slaveoutc);
- fprintf(stdout,"*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*\n");
- }
- sprintf(string,"* %2d ",(x+1));
- write(0,string,10);
- if (sb[x].reason==0)
- {
- write(0,"JT Kirk ",9);
- } else
- {
- write(0,sb[x].name,10);
- }
- write(0,ship[sb[x].sno].name,20);
- switch (sb[x].reason)
- {
- case -1:
- write(0,"Rammed the ship into a planet",29);
- break;
- case -2:
- write(0,"Ran out of energy in flight!!",29);
- break;
- case -3:
- write(0,"activated Selfdestruct & quit",29);
- break;
- case -4:
- write(0,"Blew up under heavy attack...",29);
- break;
- case -5:
- write(0,"Tried time warping twice.....",29);
- break;
- default:
- write(0,"Burnt up over Genesis........",29);
- break;
- }
- fprintf(stdout," %6d *\n",sb[x].score);
- if (x==i)
- {
- fprintf(stdout,"*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*\n");
- tputs(boff,1,slaveoutc);
- }
- }
- fprintf(stdout,"*****************************************************************************\n");
- }
-
- die()
- {
- readinscores();
- fprintf(stdout," Live long and Prosper!\n");
- unsetrawmode();
- exit(0);
- }
-
-
- /*
- * setrawmode() Function to set cbreak -echo for the terminal
- */
-
- setrawmode()
- {
- ioctl(0,TIOCGETP,¶ms); /*get terminal sgttyb structure*/
- params.sg_flags |= CBREAK;
- params.sg_flags &= ~ECHO;
- ioctl(0,TIOCSETP,¶ms); /*write out sgttyb structure*/
- signal(SIGINT, SIG_IGN);
- signal(SIGHUP, SIG_IGN);
- }
-
- /*
- * unsetrawmode() Function to set -cbreak echo for the terminal
- */
-
- unsetrawmode()
- {
- ioctl(0,TIOCGETP,¶ms); /*get terminal sgttyb structure*/
- params.sg_flags &= ~CBREAK;
- params.sg_flags |= ECHO;
- ioctl(0,TIOCSETP,¶ms); /*write out sgttyb structure*/
- signal(SIGINT, SIG_DFL);
- signal(SIGHUP, SIG_DFL);
- }
-
- gettermdata()
- {
- char *u;
- char *getenv();
-
- if((u=getenv("TERM"))==NULL) goto label1;
- if (strcmp(u,"vt200")==0) tty_type=1;
- if (strcmp(u,"a980")==0) tty_type=1;
- if (strcmp(u,"tvi910")==0) tty_type=2;
- if (strcmp(u,"tvi905")==0) tty_type=2;
- if (strcmp(u,"adm3")==0) tty_type=2;
- if (strcmp(u,"vt100")==0) tty_type=3;
- if (strcmp(u,"sun")==0) tty_type=4;
-
- label1:
-
- while ((tty_type<0) || (tty_type>4))
- {
- fprintf(stdout," ******************************************\n");
- fprintf(stdout," *PLEASE SPECIFY YOUR TERMINAL TYPE!*\n");
- fprintf(stdout," ******************************************\n");
- fprintf(stdout," *1 for vt200/adds Terminals..............*\n");
- fprintf(stdout," *2 for Televideo 910's/905's & adm3......*\n");
- fprintf(stdout," *3 for vt100.............................*\n");
- fprintf(stdout," *4 for Sun Workstations..................*\n");
- fprintf(stdout," *0 NONE OF THE ABOVE / DONT KNOW.........*\n");
- fprintf(stdout," *Input value now. If in doubt type 0.....*\n");
- fprintf(stdout," ******************************************\n");
- fprintf(stdout," Input:");
- scanf("%d",&tty_type);
- if ((tty_type<0) || (tty_type>4))
- {
- fprintf(stdout,"\n\n ******************************************\n");
- fprintf(stdout," * INVALID TERMINAL...TRY AGAIN PLASE. *\n");
- fprintf(stdout," ******************************************\n\n");
- }
- }
- if (tty_type==0)
- {
- if (gettermcapentry(cl,cm,bon,boff)!=1)
- {
- if(!termcap)
- {
- fprintf(stderr,"This means you will haveto modify the code of the program to cater for\n");
- fprintf(stderr,"your specific terminal. Otherwise you cannot play.\n");
- }
- else
- {
- fprintf(stderr,"Try again without the -t option. If you get the same result,\n");
- fprintf(stderr,"you must modify the code. (See rules)\n");
- }
- exit(0);
- }
- } else
- {
- assignterminal(tty_type,cl,cm);
- return(1);
- }
- }
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- char string[80];
- int childpid,i;
- char *s;
- char c;
- nointro=0; termcap=0;
- while(--argc>0 && (*++argv)[0]=='-')
- for (s=argv[0]+1; *s!='\0'; s++)
- switch (*s) {
- case 'i':
- nointro=1;
- break;
- case 't':
- termcap=1;
- break;
- default :
- fprintf(stderr,"Usage: mpss {-i} {-t}\n");
- exit(0);
- }
-
-
- tnam=ttyname(1);
- strcpy(terminalfile,tnam);
-
- tty_type=999;
- if ((termcap)&&(gettermcapentry(cl,cm,bon,boff)==1))
- {
- tty_type=0;
- } else
- {
- tty_type=999;
- gettermdata();
- }
- setrawmode();
-
- srand(time()%10);
- sleep(rand()%10);
-
- while ((f=fopen(LOCK,"r"))!=NULL)
- {
- fclose(f);
- fprintf(stdout," Your'e in the queue to join. Please wait..\n");
- sleep(rand()%10);
- }
-
- if ((i=creat(LOCK,0666))<0)
- fprintf(stdout," Cant create LOCK file.\n");
-
- statflg=OFF;
- /*CHECK IF THE MASTER IS ALREADY RUNNING!!!*/
- if ((f=fopen(MASTERPIDFILE,"r"))==NULL)
- {
- /*tell others the game is starting......*/
- char run[80];
- sprintf(run,"%s",MAILFILE);
- system(run);
- /*tell others the game is starting......*/
- clrscreenslave(cl,tty_type);
-
- fprintf(stdout," Invoking the game for the first player....\n");
- fprintf(stdout," This takes about 30 seconds...............\n");
- if ((pid=fork())==0) {
- execl(MASTERPROGRAM,"peer",">&!/dev/null","&",(char *) 0);
- fprintf(stderr,"cannot invoke master process!");
- exit();
- }
- statflg=ON;
- i=0;
- while ((i<20)&&(f=fopen(MASTERPIDFILE,"r"))==NULL)
- {
- fprintf(stdout," Wait a bit longer.......\n");
- sleep(5);
- i++;
- }
- if (i==20) {
- fprintf(stdout," mpss : timed out waiting.\n");
- unlink(LOCK);
- exit(0);
- }
- }
- fgets(masterpidname,15,f);
- fclose(f);
- sscanf(masterpidname,"%d\n",&masterpid);
- for (i=0;i<15;++i) masterpidname[i]='\0';
- if ((f=fopen(SHIPNOFILE,"r"))==NULL)
- {
- i=creat(SHIPNOFILE,0666);
- close(i);
- f=fopen(SHIPNOFILE,"w");
- sprintf(masterpidname,"%d\n",0);
- fputs(masterpidname,f);
- shipnumber=0;
- fclose(f);
- }
- else
- {
- fgets(masterpidname,15,f);
- fclose(f);
- sscanf(masterpidname,"%d\n",&shipnumber);
- shipnumber++;
- if (shipnumber>30) shipnumber=0;
- f=fopen(SHIPNOFILE,"w");
- sprintf(masterpidname,"%d\n",shipnumber);
- fputs(masterpidname,f);
- fclose(f);
- }
- clrscreenslave(cl,tty_type);
- if (nointro) goto nextlabel;
- poscursslave(cm,tty_type,0,0);
- tputs(bon,1,slaveoutc);
- fprintf(stdout," \n\n\n\n\n Space. \n");
- tputs(boff,1,slaveoutc);
- sleep(3);
- fprintf(stdout," The final frontier. \n");
- sleep(3);
- fprintf(stdout," These are the voyages of the Starship Enterprise,\n");
- sleep(3);
- fprintf(stdout," her ongoing mission to explore strange,new worlds.\n");
- sleep(3);
- fprintf(stdout," To seekout new lifeforms, and new civilizations,\n");
- sleep(3);
- fprintf(stdout," To boldly go where no man has gone................\n");
- sleep(4);
- fprintf(stdout," before.....\n");
- sleep(3);
- poscursslave(cm,tty_type,0,0);
- fprintf(stdout,"\n");
- fprintf(stdout,"\n");
- fprintf(stdout,"\n");
- fprintf(stdout," *****************************************************************\n");
- fprintf(stdout," * Your vessel details are as follows. *\n");
- fprintf(stdout," *****************************************************************\n");
- fprintf(stdout," * Ships Name : ");
- fprintf(stdout," %s*\n",ship[shipnumber].name);
- fprintf(stdout," * Ships Registration : ");
- fprintf(stdout," %s*\n",ship[shipnumber].registration);
- fprintf(stdout," * Class : ");
- fprintf(stdout," %s*\n",ship[shipnumber].class);
- fprintf(stdout," * Ships origin : ");
- fprintf(stdout," %s*\n",ship[shipnumber].flag);
- fprintf(stdout," * Number of crew : ");
- fprintf(stdout," %s*\n",ship[shipnumber].noofcrew);
- fprintf(stdout," * Commision date : ");
- fprintf(stdout," %s*\n",ship[shipnumber].commissiondate);
- fprintf(stdout," * Mission Objective : Blast anything in sight. *\n");
- fprintf(stdout," * *\n");
- fprintf(stdout," * Written by Peer Schmitz, University of Keele, Keele, Staffs, *\n");
- fprintf(stdout," * ST5-5BG, Jan. - Jul 88, due to a lack of STAR TREK on the box.*\n");
- fprintf(stdout," * Version 1.3.... *\n");
- fprintf(stdout," *****************************************************************\n");
- tputs(bon,1,slaveoutc);
- fprintf(stdout,"\n Hit any key\n");
- tputs(boff,1,slaveoutc);
- read(0,&c,1);
- nextlabel:
- if ((pid=fork())==0)
- {
- childpid=getpid();
- while ((f=fopen(PLAYERFILE,"r"))!=NULL)
- {
- fclose(f);
- sleep(((int)(time()%10)));
- }
- /*Now we know the file isnt there yet*/
- i=creat(PLAYERFILE,0666);
- if ((f=fopen(PLAYERFILE,"w"))==NULL)
- {
- fprintf(stdout,"CANNOT OPEN PLAYERFILE\n");
- exit(0);
- } else
- {
- char c;
- sprintf(string,"%s#%s#%s#%d#%d#%d#\n",terminalfile,cm,cl,shipnumber,childpid,tty_type);
- fputs(string,f);
- fclose(f);
- /*
- for (i=0;i<20;i++) fprintf(stderr,"*%c*",cm[i]);
- read(0,&c,1);
- fprintf(stderr,"\n\n");
- for (i=0;i<80;i++) fprintf(stderr,"*%c*",string[i]);
- read(0,&c,1);
- */
- clrscreenslave(cl,tty_type);
- kill(masterpid,29);
- while(1);
- }
- }
- while(wait(&status)!=pid);
- die();
- }
-